<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>MongoDB 连接和用户管理 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>MongoDB 连接和用户管理</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/MongoDB/">MongoDB</a>
			</span>
		
	</div>

	

	
		<h2 id="1-连接-MongoDB"><a href="#1-连接-MongoDB" class="headerlink" title="1.连接 MongoDB"></a>1.连接 MongoDB</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在本机可以直接运行命令mongo进入到mongodb shell中</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/01.png?raw=true" alt="01"><figcaption class="figure__caption">01</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果mongodb监听端口并不是默认的27017，则在连接的时候需要加–port 选项，例如</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/02.png?raw=true" alt="02"><figcaption class="figure__caption">02</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;连接远程mongodb，需要加–host，例如</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/03.png?raw=true" alt="03"><figcaption class="figure__caption">03</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果设置了验证，则在连接的时候需要带用户名和密码</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mongo -uusername -ppasswd  <span class="comment">#这个和MySQL挺像</span></div></pre></td></tr></table></figure>
<h2 id="2-MongoDB-用户管理"><a href="#2-MongoDB-用户管理" class="headerlink" title="2. MongoDB 用户管理"></a>2. MongoDB 用户管理</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先mongodb用户是针对库来的，建立用户时要先进入到对应的库里</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">use <span class="built_in">test</span>  <span class="comment">#切换到test库下</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/04.png?raw=true" alt="04"><figcaption class="figure__caption">04</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建用户</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&gt; db.createUser(&#123;user:<span class="string">"admin"</span>,<span class="built_in">pwd</span>:<span class="string">"123456"</span>,roles:[&#123;role:<span class="string">'dbOwner'</span>,db:<span class="string">'userdb'</span>&#125;]&#125;)</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/05.png?raw=true" alt="05"><figcaption class="figure__caption">05</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;use admin //切换到admin库</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/06.png?raw=true" alt="06"><figcaption class="figure__caption">06</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;db.system.users.find()  //列出所有用户，需要切换到admin库</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/07.png?raw=true" alt="07"><figcaption class="figure__caption">07</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;show users  //查看当前库下所有的用户</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/08.png?raw=true" alt="08"><figcaption class="figure__caption">08</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;db.dropUser(‘admin’) //删除用户</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/09.png?raw=true" alt="09"><figcaption class="figure__caption">09</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;关于用户角色，参考文档  <a href="">Mongodb 3.0 配置身份验证db.createUser()说明</a></p>
<h2 id="3-MongoDB-库管理"><a href="#3-MongoDB-库管理" class="headerlink" title="3.MongoDB 库管理"></a>3.MongoDB 库管理</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;db.version() // 查看版本</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/10.png?raw=true" alt="10"><figcaption class="figure__caption">10</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;use userdb // 如果库存在就切换，不存在就创建</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/11.png?raw=true" alt="11"><figcaption class="figure__caption">11</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;show dbs  // 查看库，此时userdb并没有出现，这是因为该库是空的，还没有任何集合，只需要创建一个集合就能看到了</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/12.png?raw=true" alt="12"><figcaption class="figure__caption">12</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;db.createCollection(‘clo1’) // 创建集合clo1，在当前库下面创建</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/13.png?raw=true" alt="13"><figcaption class="figure__caption">13</figcaption></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/14.png?raw=true" alt="14"><figcaption class="figure__caption">14</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;db.dropDatabase() // 删除当前库，要想删除某个库，必须切换到那个库下</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/15.png?raw=true" alt="15"><figcaption class="figure__caption">15</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;db.stats() // 查看当前库的信息</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/16.png?raw=true" alt="16"><figcaption class="figure__caption">16</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;db.serverStatus() // 查看mongodb服务器的状态</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/17.png?raw=true" alt="17"><figcaption class="figure__caption">17</figcaption></figure></p>
<h2 id="4-MongoDB-创建集合"><a href="#4-MongoDB-创建集合" class="headerlink" title="4.MongoDB 创建集合"></a>4.MongoDB 创建集合</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&gt; db.createCollection(<span class="string">"mycol"</span>, &#123; capped : <span class="literal">true</span>, autoIndexID : <span class="literal">true</span>, size : 6142800, max : 10000 &#125; )</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;语法：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">db.createCollection(name,options)</div></pre></td></tr></table></figure>
<ul>
<li><p>name就是集合的名字，options可选，用来配置集合的参数，参数如下</p>
</li>
<li><p>capped true/false （可选）如果为true，则启用封顶集合。封顶集合是固定大小的集合，当它达到其最大大小，会自动覆盖最早的条目。如果指定true，则也需要指定尺寸参数。</p>
</li>
<li><p>autoindexID  true/false （可选）如果为true，自动创建索引_id字段的默认值是false。</p>
</li>
<li><p>size （可选）指定最大大小字节封顶集合。如果封顶如果是 true，那么你还需要指定这个字段。单位B</p>
</li>
<li><p>max （可选）指定封顶集合允许在文件的最大数量。</p>
</li>
</ul>
<h2 id="5-MongoDB-数据管理"><a href="#5-MongoDB-数据管理" class="headerlink" title="5.MongoDB 数据管理"></a>5.MongoDB 数据管理</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;show collections //查看集合，或者使用show  tables</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/18.png?raw=true" alt="18"><figcaption class="figure__caption">18</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;db.Account.insert({AccountID:1,UserName:”123”,password:”123456”})  //如果集合不存在，直接插入数据，则mongodb会自动创建集合</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/19.png?raw=true" alt="19"><figcaption class="figure__caption">19</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;db.Account.update({AccountID:1},{“$set”:{“Age”:20}}) //更新</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/20.png?raw=true" alt="20"><figcaption class="figure__caption">20</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;db.Account.find()   //查看所有文档</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/21.png?raw=true" alt="21"><figcaption class="figure__caption">21</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;db.Account.find({AccountID:1})   //根据条件查询</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/22.png?raw=true" alt="22"><figcaption class="figure__caption">22</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;db.Account.remove({AccountID:1})  //根据条件删除</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/23.png?raw=true" alt="23"><figcaption class="figure__caption">23</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;db.Account.drop() //删除所有文档，即删除集合</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/24.png?raw=true" alt="24"><figcaption class="figure__caption">24</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;use dbname  //先进入对应的库</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;db.printCollectionStats()  // 然后查看集合状态</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86/25.png?raw=true" alt="25"><figcaption class="figure__caption">25</figcaption></figure></p>

	

	
		<span class="different-posts"><a href="/2017/10/12/MongoDB/3. MongoDB 连接和用户管理/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
